import { reactive } from 'vue'
import { getRoleOptionList } from '@/api/system/role'
import { addUser, updateUser, getUserForm, getUserPassword } from '@/api/system/user'

export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })

  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.userCode ? await updateUser(values.userCode, values) : await addUser(values)
      status.saving = false
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }

  const getRoleList = async () => {
    return getRoleOptionList()
  }

  const getUserPasswordByCode = async (userCode: any) => {
    return getUserPassword(userCode)
  }

  const getUserById = async (userCode: any) => {
    return getUserForm(userCode)
  }
  return { status, createOrUpdate, getUserById, getRoleList, getUserPasswordByCode }
}
